<template>
  <el-dialog
    v-el-drag-dialog
    title="拉黑"
    :visible.sync="visible"
    width="600px"
    destroy-on-close
    :append-to-body="true"
    @closed="reset"
  >
    <el-form ref="dataForm" :model="temp" :rules="rules" label-position="right" label-width="100px">
      <el-form-item label="拉黑原因" prop="blackKey">
        <el-select v-model="temp.blackKey" placeholder="请选择原因">
          <el-option
            v-for="item in blackKeyList"
            :key="item.keyId"
            :label="item.keyName"
            :value="item.keyName"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="remark">
        <el-input v-model="temp.remark" type="textarea" :rows="3" :maxlength="maxLength" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryBlackKeyList, updateCsmStatus } from '@/api'

  export default {
    name: 'blacklistCsm',
    data() {
      return {
        visible: false,
        blackKeyList: [{ keyName: '其它', keyId: -1 }],
        csmId: '',
        temp: {
          blackKey: '',
          remark: ''
        },
        rules: {
          blackKey: [{ required: true, message: '请选择原因', trigger: 'change' }]
        },
        maxLength: 150
      }
    },
    methods: {
      async open(temp) {
        console.log(temp)
        const res = await queryBlackKeyList()
        this.blackKeyList = [...res.data, ...this.blackKeyList]
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.csmId = temp.csmId
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const remark = this.temp.blackKey + this.temp.remark
          const params = { csmId: this.csmId, csmStatus: 4, remark }
          this.submit(params)
        })
      },
      async submit(params) {
        await updateCsmStatus(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.csmId = ''
        this.temp = {
          remark: '',
          blackKey: ''
        }
        this.blackKeyList = [{ keyName: '其它', keyId: -1 }]
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
